package net.pws.common.persistence.utils;

import java.util.Map;

import net.pws.common.persistence.BeanNotFoundException;
import net.pws.common.persistence.EntityManager;

import org.springframework.context.ApplicationContext;


public abstract class PersistenceHelper {
    
    public static EntityManager getEntityManager(ApplicationContext applicationContext) {
        Object result = applicationContext.getBean("entityManager");
        if (result == null) {
            Map beans = applicationContext.getBeansOfType(EntityManager.class);
            if (beans.isEmpty()) {
                throw new BeanNotFoundException("EntityManager is not found in ApplicationContext!");
            }
            return (EntityManager) ((Map.Entry) beans.entrySet()
                                                     .iterator()
                                                     .next()).getValue();
        }
        return (EntityManager) result;
    }
    
}
